{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. Asyncio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculation takes 0.36158549999890965 seconds\n",
      "CPU times: total: 93.8 ms\n",
      "Wall time: 363 ms\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import concurrent.futures\n",
    "def cpu_bound(number):\n",
    "    print(sum(i * i for i in range(number)))\n",
    "\n",
    "def calculate_sums(numbers):\n",
    "    # for number in numbers:\n",
    "    #     cpu_bound(number)\n",
    "    # with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:\n",
    "    #   executor.map(cpu_bound,numbers)\n",
    "    \n",
    "    with concurrent.futures.ProcessPoolExecutor() as executor:\n",
    "      executor.map(cpu_bound,numbers)\n",
    "\n",
    "def main():\n",
    "    start_time = time.perf_counter()  \n",
    "    numbers = [10000000 + x for x in range(20)]\n",
    "    calculate_sums(numbers)\n",
    "   \n",
    "\n",
    "\n",
    "    end_time = time.perf_counter()\n",
    "    print('Calculation takes {} seconds'.format(end_time - start_time))\n",
    "    \n",
    "\n",
    "%time main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
